//
//  WNCancelVC.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/1.
//

import UIKit

class WNCancelVC: WNBaseVC {

    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "If you delete use account, you will not be able to log in to the account and all the retained information will be cleared and cannot be retrieved."
        label.textColor = .init(named: "black_333333")
        label.font = .wn_font(name: .bold, size: 16)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var desLabel: UILabel = {
        let label = UILabel()
        label.text = "Including the following information:"
        label.textColor = .black
        label.font = .wn_font(name: .regular, size: 14)
        return label
    }()
    
    lazy var cancelBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Confirm Delete", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.rx.tap.bind { [weak self] in
            self?.accountCancel()
        }.disposed(by: rx.disposeBag)
        btn.backgroundColor = WNColor.Fade().left2right(.init(width: scaleWidth(240), height: 44))
        btn.wn_cornerWithBorder(22)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewConfig() {
        view.addSubview(titleLabel)
        view.addSubview(desLabel)
        view.addSubview(cancelBtn)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(NaviTotalHeight+20)
            make.left.equalTo(12)
            make.right.equalTo(-12)
        }
        desLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(14)
            make.left.equalTo(12)
        }
        cancelBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-TabBarMargin-42)
            make.width.equalTo(scaleWidth(240))
            make.height.equalTo(44)
        }
    }
    
    override func dataConfig() {
        pageName = "Delete Account"
        navView.titleName = "Delete Account"
        let titleArr = ["Viewed", "Comment content", "User Info", "User Collect", "Gold balance"]
        let iamgeArr = ["setting_cancel_history", "setting_cancel_msg", "setting_cancel_user", "setting_cancel_like", "setting_cancel_gold"]
        for i in 0..<5 {
            let cancelView = CancelView()
            cancelView.title.text = titleArr[i]
            cancelView.iconImage.image = UIImage(named: iamgeArr[i])
            view.addSubview(cancelView)
            cancelView.snp.makeConstraints { make in
                make.left.equalTo(12)
                make.height.equalTo(42)
                make.top.equalTo(desLabel.snp.bottom).offset(10+42*i)
            }
        }
    }
    
    func accountCancel() {
        showSpringType(.deleteAccount)
        WNSpringView.shared.isRightClosure = { [weak self] isRight in
            if !isRight {
                WNHUD.wn_showLoading()
                provider_setting.rx.request(.deleteUser)
                    .asObservable().mapModel(BaseModel<Any>.self)
                    .subscribe(onNext: { (model) in
                        guard model.code == 0 else {
                            self?.wn_toast(model.msg ?? "")
                            return
                        }
                        WNHUD.wn_hide()
                        hidenLibRec()
                        hidenTabLast()
                        setKeyUserId("")
                        saveUser(user: nil)
                        clearHistory()
                        saveDarkMode(0)
                        saveHasCheck(false)
                        saveAdJson([:])
                        saveTapAD(false)
                        saveIsDeeplink(false)
                        saveAutoOpen(false)
                        saveReadConfig(["brightness": Float(UIScreen.main.brightness), "size": 16, "space": 5.0, "bgColor": "FFFFFF", "font": "regular", "flip": "Page"])
                        WNSingleData.shared.userGold = 0
                        NotificationCenter.default.post(name: WNNotiName.loginNoti, object: nil)
                        self?.navigationController?.popToRootViewController(animated: true)
                        wn_getTopVC()?.tabBarController?.tabBar.isHidden = false
                        WNThread.wn_delay(0.1) {
                            wn_getTopVC()?.tabBarController?.selectedIndex = 1
                        }
                    }, onError: { (error) in
                        self?.wn_toast("Check the network connection")
                    }).disposed(by: self!.rx.disposeBag)
            }
        }
    }
}
